package com.bloveoacom.weichi.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.bloveoacom.base.BaseList2Activity;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.Constant;
import com.bloveoacom.util.DataUtil;
import com.bloveoacom.util.GlideUtil;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.MyFansBean;
import com.bloveoacom.weichi.bean.OptionBean;
import com.bloveoacom.weight.ClickImageView;
import com.bloveoacom.weight.LlkjToolBar;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;


import java.util.List;

import butterknife.BindView;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/7/12
 * desc  : 我的关注或者粉丝
 */
public class MyAttentionActivity extends BaseList2Activity<MyFansBean> {

    @BindView(R.id.title_bar)
    LlkjToolBar titleBar;
    private String user_id,title;

       //  0 粉丝 1 关注
    private int type;

    @Override
    public int getLayoutResId() {
        return R.layout.layout_refresh_title;
    }

    @Override
    public void initView() {
        super.initView();
        titleBar.setToolBar(title,
                true, "", R.drawable.ic_back, true, "", LlkjToolBar.isNull);
    }


    @Override
    protected View getViewSetTB() {
        return titleBar;
    }

    @Override
    public void getBundleExtras(Bundle extras) {
        type = getIntent().getIntExtra(Constant.DATA0,0);
        user_id = getIntent().getStringExtra(Constant.DATA1);
        title = getIntent().getStringExtra(Constant.DATA2);
    }



    @Override
    protected int getItemLayout() {
        return R.layout.rv_item_fans;
    }

    @Override
    protected void convertView(BaseViewHolder helper, MyFansBean item) {
        GlideUtil.loadCircleImage(helper.getView(R.id.iv_head), StringUtils.getFullUrl(item.headImg,0));
        helper.setText(R.id.tv_username,item.userName);
        helper.setText(R.id.tv_describ, DataUtil.getSignature(item.signature));
        if(Integer.valueOf(item.isFollow) == 1){
            helper.setText(R.id.tv_attention,"已关注");
            helper.setTextColor(R.id.tv_attention, ContextCompat.getColor(mContext,R.color.color_858585));
        }else if(Integer.valueOf(item.isFollow) == 2){
            helper.setText(R.id.tv_attention,"互相关注");
            helper.setTextColor(R.id.tv_attention, ContextCompat.getColor(mContext,R.color.color_858585));
        }else{
            helper.setText(R.id.tv_attention,"+关注");
            helper.setTextColor(R.id.tv_attention, ContextCompat.getColor(mContext,R.color.colorPrimary));
        }
        helper.getView(R.id.tv_attention).setSelected(Integer.valueOf(item.isFollow) != 0);
        helper.addOnClickListener(R.id.tv_attention);
        ((ClickImageView) helper.getView(R.id.iv_head)).setData(new OptionBean(item.userId));
    }



    @Override
    protected void getData() {
        if(type==1){
            BaseSubscriber2<List<MyFansBean>> baseSubscriber = HttpMethods.getInstance().getHttpService().queryUserFocusList(UserInfoBean.getInstance().getToken(), user_id, page, HttpMethods.PAGE_SIZE).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<List<MyFansBean>>(this) {
                @Override
                public void onNext(List<MyFansBean> o) {
                    doData(o);
                }
            });
            addDispose(baseSubscriber);
        }else{
            BaseSubscriber2<List<MyFansBean>> baseSubscriber =   HttpMethods.getInstance().getHttpService().queryUserFansList(UserInfoBean.getInstance().getToken(),user_id,page,HttpMethods.PAGE_SIZE).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<List<MyFansBean>>(this){
                @Override
                public void onNext(List<MyFansBean> o) {
                    doData(o);
                }
            });
            addDispose(baseSubscriber);
        }

    }


    MyFansBean bean;
    @Override
    public void initListener() {
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                 bean = datas.get(position);
                switch (view.getId()) {
                    case R.id.tv_attention:
                        DisposableSubscriber<Object> disposableSubscriber = HttpMethods.getInstance().getHttpService().userFocus(UserInfoBean.getInstance().getToken(), bean.userId, (Integer.valueOf(bean.isFollow) == 0 ? 0 : 1)).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>() {
                            @Override
                            public void onNext(Object o) {
                                //取消关注
                                if(Integer.valueOf(bean.isFollow) == 1){
                                    datas.remove(bean);
                                }else{
                                    //关注
                                    bean.isFollow = "1";
                                }
                                adapter.notifyDataSetChanged();
                            }

                        });
                        addDispose(disposableSubscriber);
                        break;
                }
            }
        });

    }


    /**
     *
     * @param context
     * @param type
     * 0 粉丝 1 关注
     * @param user_id
     * 查询某人的用户id
     */
    public static void startA(Context context, int type, String user_id, String title){
        Intent intent = new Intent(context,MyAttentionActivity.class);
        intent.putExtra(Constant.DATA0,type);
        intent.putExtra(Constant.DATA1,user_id);
        intent.putExtra(Constant.DATA2,title);
        context.startActivity(intent);

    }

}
